package com.shy.servlet;

import com.shy.model.Student;
import com.shy.service.StudentService;
import com.shy.service.impl.StudentServiceImpl;
import com.shy.model.Page;

import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 按照页码查询该页面的数据
 *
 */
@WebServlet("/sblbp")
public class SearchBatchListByPageServlet extends HttpServlet {

    StudentService studentService = new StudentServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //1. 获取请求参数
        String batchName = req.getParameter("batchName");
        int pageNo = req.getParameter("pageNo") == null ? Page.DEFAULT_PAGE_NO : Integer.parseInt(req.getParameter("pageNo"));

        //2. 调用业务逻辑 ，完成请求处理
        int totalRows = studentService.getBatchSize(batchName);
        List<Student> list = studentService.findStudentsByBatchNameAndPage(batchName,pageNo,Page.DEFAULT_PAGE_SIZE);
        Page page = new Page(pageNo,totalRows,list);
        //3. 生成动态响应
        req.setAttribute("page", page);
        req.getRequestDispatcher("/student_list.jsp").forward(req, resp);
    }
}
